using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HealthSmart;
using System.Data;
using NYProviders.Models;
using NYProviders.DataRules;

namespace $PROJECT_NAMESPACE$.BusinessRules
{
	/// <summary>
	/// Data access layer class for $CLASS_NAME$
	/// </summary>
	class $CLASS_NAME$BR
	{
		$CLASS_NAME$DR _dr = new $CLASS_NAME$DR();
		
		
        #region Constructor

		/// <summary>
		/// Class constructor
		/// </summary>
		public $CLASS_NAME$BR()
		{
			// Nothing for now.
		}

        #endregion

        #region Public Methods

        /// <summary>
        /// insert new row in the table
        /// </summary>
		/// <param name="businessObject">business object</param>
		/// <returns>true of successfully insert</returns>
		public SQLQueryStatus Insert($CLASS_NAME$ businessObject)
		{
	           SQLQueryStatus status = new SQLQueryStatus();	

                try 
	{	        
		status = _dr.Insert(businessObject);
	}
	catch ()
	{
		
	}
        return status;

		}

         /// <summary>
        /// update row in the table
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <returns>true for successfully updated</returns>
        public SQLQueryStatus Update($CLASS_NAME$ businessObject)
		{
	           SQLQueryStatus status = new SQLQueryStatus();	

                try 
	{	        
		status = _dr.Update(businessObject);
	}
	catch ()
	{
		
	}
        return status;

		}

        /// <summary>
        /// Select by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>$CLASS_NAME$ business object</returns>
        public $CLASS_NAME$ SelectByPrimaryKey($CLASS_NAME$Keys keys)
        {
            try
            {
                DataSet ds = _dr.SelectByPrimaryKey(keys);
                IDataReader dataReader = ds.CreateDataReader();

                if (dataReader.Read())
                {
                    $CLASS_NAME$ businessObject = new $CLASS_NAME$();

                    PopulateBusinessObjectFromReader(businessObject, dataReader);

                    return businessObject;
                }
                else
                {
                    return null;
                }
            }catch{

                }
        }

        /// <summary>
        /// Select all rescords
        /// </summary>
        /// <returns>list of $CLASS_NAME$</returns>
        public List<$CLASS_NAME$> SelectAll()
        {
             try
            {
                DataSet ds = _dr.SelectByPrimaryKey(keys);
                IDataReader dataReader = ds.CreateDataReader();

                if (dataReader.Read())
                {
                    return PopulateObjectsFromReader(dataReader);
                }
                else
                {
                    return null;
                }
            }catch{
            }

        }

        /// <summary>
        /// Select records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>list of $CLASS_NAME$</returns>
        public List<$CLASS_NAME$> SelectByField(string fieldName, object value)
        {

           try
            {
                DataSet ds = _dr.SelectByField(fieldName, value);
                IDataReader dataReader = ds.CreateDataReader();

                if (dataReader.Read())
                {
                    return PopulateObjectsFromReader(dataReader);
                }
                else
                {
                    return null;
                }
            }catch{
            }


        }

        /// <summary>
        /// Delete by primary key
        /// </summary>
        /// <param name="keys">primary keys</param>
        /// <returns>true for successfully deleted</returns>
        public SQLQueryStatus Delete($CLASS_NAME$Keys keys)
        {
            SQLQueryStatus status = new SQLQueryStatus();	

                try 
	{	        
		status = _dr.Delete(keys);
	}
	catch ()
	{
		
	}
        return status;

        }

        /// <summary>
        /// Delete records by field
        /// </summary>
        /// <param name="fieldName">name of field</param>
        /// <param name="value">value of field</param>
        /// <returns>true for successfully deleted</returns>
        public bool DeleteByField(string fieldName, object value)
        {
            SQLQueryStatus status = new SQLQueryStatus();	

                try 
	{	        
		status = _dr.DeleteByField(fieldName, value);
	}
	catch ()
	{
		
	}
        return status;

        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Populate business object from data reader
        /// </summary>
        /// <param name="businessObject">business object</param>
        /// <param name="dataReader">data reader</param>
        internal void PopulateBusinessObjectFromReader($CLASS_NAME$ businessObject, IDataReader dataReader)
        {

            $POPULATE_BUSINESS_OBJECT_PARAMERTERS$

        }

        /// <summary>
        /// Populate business objects from the data reader
        /// </summary>
        /// <param name="dataReader">data reader</param>
        /// <returns>list of $CLASS_NAME$</returns>
        internal List<$CLASS_NAME$> PopulateObjectsFromReader(IDataReader dataReader)
        {

            List<$CLASS_NAME$> list = new List<$CLASS_NAME$>();

            while (dataReader.Read())
            {
                $CLASS_NAME$ businessObject = new $CLASS_NAME$();
                PopulateBusinessObjectFromReader(businessObject, dataReader);
                list.Add(businessObject);
            }
            return list;

        }

        #endregion

	}
}
